import { _decorator, Animation, AnimationClip } from 'cc'
import State from '../../Base/State'
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
import { EntityTypeEnum } from '../../Common'
import { EntityStateEnum, ParamsNameEnum } from '../../Enum'
import { ObjectPoolManager } from '../../Global/ObjectPoolManager'
const { ccclass } = _decorator

@ccclass('ExplosionStateMachine')
export class ExplosionStateMachine extends StateMachine {
  init(type: EntityTypeEnum) {
    this.type = type
    this.animationComponent = this.node.addComponent(Animation)
    this.initParams()
    this.initStateMachines()
    this.initAnimationEvent()
  }

  initParams() {
    this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger())
  }

  initStateMachines() {
    this.stateMachines.set(
      ParamsNameEnum.Idle,
      new State(this, `${this.type}${EntityStateEnum.Idle}`, AnimationClip.WrapMode.Normal)
    )
  }

  // 爆炸动画结束时销毁爆炸
  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, () => {
      // 回收对象池 == 销毁爆炸
      ObjectPoolManager.Instance.ret(this.node)
    })
  }

  run() {
    switch (this.currentState) {
      case this.stateMachines.get(ParamsNameEnum.Idle):
        if (this.params.get(ParamsNameEnum.Idle).value) {
          this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
        } else {
          this.currentState = this.currentState
        }
        break
      default:
        this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
        break
    }
  }
}
